const app = getApp()

Page({
    data: {
        address: {},
        prompt: {
            hidden: 0,
            icon: '../../../assets/images/iconfont-addr-empty.png',
            title: '还没有收货地址呢',
            text: '请去设置中添加地址',
        },
    },
    onLoad(option) {
        this.address = app.HttpResource('/address')
        this.order = app.HttpResource('/order/:id', { id: '@id' })
    		this.setData({
    			orderId: option.id
    		});
    },
    onShow() {
        this.onPullDownRefresh()
    },
    initData() {
        this.setData({
            address: {
                items: []
            }
        })
    },
    toAddressAdd(e) {
        app.WxService.navigateTo('/pages/setting/address/add/index')
    },
    selectAddress(e) {
        const id = this.data.orderId
    		const params = {
            addressId: e.currentTarget.dataset.id
        }
    		this.order.updateAsync({ id: id },params).then(data => {
          app.WxService.navigateTo('/pages/query/order/index', {
              id: this.data.orderId
          })
    		})
    },
    getAddressList() {
        const address = this.data.address
        const params = address.params

        // App.HttpService.getAddressList(params)
        this.address.queryAsync(params)
            .then(data => {
                console.log(data)
                address.items = data
                this.setData({
                    address: address,
                    'prompt.hidden': address.items.length,
                })
            })
    },
    onPullDownRefresh() {
        this.initData()
        this.getAddressList()
    }
})
